﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using com.vs.General;

namespace com.vs.General.BL
{
    public class LicenseKeyParser
    {
        public LicenseKeyParser()
        {
        }

        #region ReplaceLicenseKeyExpirationDate
        /// <summary>
        /// This method replaces the current license key expiration date with the new expiration date supplied
        /// </summary>
        public string ReplaceLicenseKeyExpirationDate(Session session, string licenseKey, DateTime expirationDate)
        {
            return this.ReplaceLicenseKeyComponent(
                session,
                licenseKey,
                DateManager.ToStringWithInvariantCulture(expirationDate),
                LicenseKeyConstants.iKEY_EXPIRATION_DATE);
        }
        #endregion ReplaceLicenseKeyExpirationDate

        #region ReplaceLicenseKeyModules
        /// <summary>
        /// This method replace the license key with the new license key supplied
        /// </summary>
        /// <param name="session"></param>
        /// <param name="licenseKey">License key to be replaced</param>
        /// <param name="newModules">Replacement license key</param>
        /// <returns>license after replacement</returns>
        public string ReplaceLicenseKeyModules(Session session, string licenseKey, string newLicenseKey)
        {
            return this.ReplaceLicenseKeyComponent(session, licenseKey, newLicenseKey, LicenseKeyConstants.iKEY_MODULES);
        }
        #endregion

        #region ReplaceLicenseKeyComponent
        private string ReplaceLicenseKeyComponent(Session session, string licenseKey, string replacementValue, int componentPosition)
        {
            string component = null;
            string[] requiredLicenseComponents;
            string[] licenseComponents;
            System.Text.StringBuilder license = new System.Text.StringBuilder();

            //From the licensekeystring Get all the licensecompnents in an array
            licenseComponents = StringManager.Split(licenseKey, new String[] { LicenseKeyConstants.stDODELIM });
            component = licenseComponents[componentPosition];

            //Split the identifier from the value, and retrieve the value
            requiredLicenseComponents = StringManager.Split(component, LicenseKeyConstants.stCOLON);

            //Next replace the component value
            requiredLicenseComponents[1] = replacementValue;

            //Add the prefix to the new modules value
            component = requiredLicenseComponents[0] + LicenseKeyConstants.stCOLON + requiredLicenseComponents[1];

            //Finally put the modules back in the license key itself
            licenseComponents[componentPosition] = component;

            //Create the license in a string format once again
            for (int i = 0; i < licenseComponents.Length; i++)
            {
                if (i > 0)
                    license.Append(Constants.stDODELIM);

                license.Append(licenseComponents[i]);
            }

            return license.ToString();
        }
        #endregion

        /// <summary>
        /// Method to Retrieve the Bios Serial Number from the License Key
        /// </summary>
        /// <param name="session"></param>
        /// <param name="stLicenseKey"></param>
        /// <returns></returns>
        public string rtrvBiosSerialNum(Session session, string stLicenseKey)
        {
            return this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_BIOS_SERIAL_NUM);
        }//end rtrvBiosSerialNum

        /// <summary>
        /// Method to retrieve the Creation Date from the LicenseKey
        /// </summary>
        /// <param name="session"></param>
        /// <param name="stLicenseKey"></param>
        /// <returns></returns>
        public DateTime RtrvCreationDate(Session session, string stLicenseKey)
        {
            string date = this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_CREATION_DATE);

            if (StringManager.IsEmpty(date))
                return DateTime.MinValue;
            else
                return DateManager.ParseWithInvariantCulture(date);
        }

        /// <summary>
        /// Method to retrieve the Expiration Action from the LicenseKey
        /// </summary>
        /// <param name="session"></param>
        /// <param name="stLicenseKey"></param>
        /// <returns></returns>
        public string rtrvExpirationAction(Session session, string stLicenseKey)
        {
            string stExpirationAction = null;
            stExpirationAction = this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_EXPIRATION_ACTION);
            return stExpirationAction;
        }

        /// <summary>
        /// Method to retrieve the Expiration Action from the LicenseKey
        /// </summary>
        /// <param name="session"></param>
        /// <param name="stLicenseKey"></param>
        /// <returns></returns>
        public DateTime rtrvExpirationDate(Session session, string stLicenseKey)
        {
            DateTime dtExpirationDate = new DateTime();
            string stDate = null;

            stDate = this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_EXPIRATION_DATE);
            dtExpirationDate = DateManager.ParseWithInvariantCulture(stDate);
            return dtExpirationDate;
        }

        /// <summary>
        /// Method to retrieve the License Type from the LicenseKey
        /// </summary>
        /// <param name="stLicenseKey"></param>
        /// <returns></returns>
        public LicenseKeyConstants.LicenseKeyType RtrvLicenseType(string stLicenseKey)
        {
            string licenseType = this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_LICENSE_TYPE);

            if (string.IsNullOrEmpty(licenseType))
                return LicenseKeyConstants.LicenseKeyType.None;

            return (LicenseKeyConstants.LicenseKeyType)Enum.Parse(typeof(LicenseKeyConstants.LicenseKeyType), licenseType, true);
        }

        /// <summary>
        /// Method to retrieve the Expiration Action from the LicenseKey
        /// </summary>
        /// <param name="session"></param>
        /// <param name="stLicenseKey"></param>
        /// <returns></returns>
        public string rtrvMachineName(Session session, string stLicenseKey)
        {
            return this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_MACHINE_NAME);
        }

        /// <summary>
        /// Method to retrieve the Expiration Action from the LicenseKey
        /// </summary>
        /// <param name="session"></param>
        /// <param name="stLicenseKey"></param>
        /// <returns></returns>
        public string rtrvModules(Session session, string stLicenseKey)
        {
            string stModules = null;
            stModules = this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_MODULES);
            if (StringManager.IsEmpty(stModules))
                return null;
            else
                return stModules;
        }

        /// <summary>
        /// Method to retrieve the License Type from the LicenseKey
        /// </summary>
        /// <param name="stLicenseKey"></param>
        /// <returns></returns>
        public int RtrvNumberOfUsers(string stLicenseKey)
        {
            return Convert.ToInt32(this.rtrvLicenseComponent(stLicenseKey, LicenseKeyConstants.iKEY_NUMBER_OF_USERS));
        }

        /// <summary>
        /// This method retrieves the value of the required licensekey component.
        /// </summary>
        /// <param name="stLicenseKey"></param>
        /// <param name="nComponentPosition"></param>
        /// <returns>Value of the licensekey component</returns>
        private string rtrvLicenseComponent(string licenseKey, int componentPosition)
        {

            string stRequiredLicenseComponent = null;
            string stTemp = null;
            string[] stArrRequiredLicenseComponent;
            string[] stArrLicenseComponents;

            //From the licensekeystring Get all the licensecompnents in an array
            stArrLicenseComponents = StringManager.Split(licenseKey, new String[] { LicenseKeyConstants.stDODELIM });

            if (stArrLicenseComponents.Length < componentPosition + 1)
                return null;

            stTemp = stArrLicenseComponents[componentPosition];

            //Split the identifier from the value, and retrieve the value
            stArrRequiredLicenseComponent = StringManager.Split(stTemp, LicenseKeyConstants.stCOLON);

            stRequiredLicenseComponent = stArrRequiredLicenseComponent[1];

            return stRequiredLicenseComponent;
        }
    }//End of class
}//End of namespace